//
//  Table.swift
//  iOSProjectExtensionSwift
//
//  Created by HBGG on 2020/8/31.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit
import Alamofire
import GTMRefresh
extension UITableView {

    static func createTabelView(_ cellClass:AnyClass?, _ identifier:String,_ delegate:Any) -> UITableView {
        let table = UITableView(frame: CGRect(x: 0, y: 0, width: DeviceWidth, height:  DeviceHeight-Height_NavBar), style:UITableView.Style.plain)
        table.delegate = delegate as? UITableViewDelegate
        table.dataSource = delegate as? UITableViewDataSource
        table.isOpaque = true//原因是这会使系统用一个最优的方式渲染这些views
        table.separatorStyle = UITableViewCell.SeparatorStyle.none;
        table.backgroundColor = UIColor.clear;
        table.register(cellClass, forCellReuseIdentifier: identifier)
        //去除动画异常
        if #available(iOS 11.0, *) {
            table.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never;
            table.estimatedRowHeight = 0;
            table.estimatedSectionHeaderHeight = 0;
            table.estimatedSectionFooterHeight = 0;
        }
        setExtraCellLineHidden(tableView:table)
        checkNetWorkStateValue(table: table)
        return table
    }
    static func setExtraCellLineHidden(tableView:UITableView) {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        tableView.tableFooterView = view
        
    }
    //网络监听
    static func checkNetWorkStateValue(table:UITableView) {
        let reachability = Reachability(hostName: "www.apple.com")
        let netListner = NetListenViewController()
        if(reachability?.currentReachabilityStatus() == NotReachable) {
            CBToast.showToastAction(message: "请检查您的网络")
            //展示网络错误的界面
            netListner.goSpecialViewController(tab: table)
        }
        
        netListner.networkStateChangeNSNotificationCenterWithTabelView(table: table)
    }
}
